<template>
  <div>
    <!--引用表格-->
    <BasicTable @register="registerTable">
      <!--插槽:table标题-->
      <template #tableTitle>
        <a-button type="primary" @click="handleAdd" preIcon="ant-design:plus-outlined"> 新增</a-button>
      </template>
      <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" :dropDownActions="getDropDownAction(record)" />
      </template>
      <!--字段回显插槽-->
      <template #htmlSlot="{ text }">
        <div v-html="text"></div>
      </template>
      <template #proportion="{ text }">
        <div>
          <span>{{ text.proportion }}</span>
          <span>({{ text.proportion * 100 }}%)</span>
        </div>
      </template>
      <template #img="{ text }">
        <TableImg v-if="text" :size="60" :simpleShow="true" :imgList="text" />
      </template>
      <!--省市区字段回显插槽-->
      <template #pcaSlot="{ text }">
        {{ getAreaTextByCode(text) }}
      </template>
      <template #fileSlot="{ text }">
        <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
        <a-button v-else :ghost="true" type="primary" preIcon="ant-design:download-outlined" size="small"
          @click="downloadFile(text)">下载</a-button>
      </template>
    </BasicTable>
    <!-- 表单区域 -->
    <TGrowthcycleModal @register="registerModal" @success="handleSuccess"></TGrowthcycleModal>
    <GrowthcyclePropertitesTable @register="growthcyclePropertitesDrawer"></GrowthcyclePropertitesTable>
    <Desc @register="registerDesc"></Desc>
  </div>
</template>

<script lang="ts" name="TGrowthcycle-tGrowthcycle" setup>
import { ref, computed, unref } from 'vue';
import { BasicTable, useTable, TableAction, TableImg } from '/@/components/Table';
import { useModal } from '/@/components/Modal';
import { useDrawer } from '/@/components/Drawer';
import { useListPage } from '/@/hooks/system/useListPage';
import TGrowthcycleModal from './components/TGrowthcycleModal.vue';
import GrowthcyclePropertitesTable from "./components/GrowthcyclePropertitesTable.vue";
import { columns, searchFormSchema } from './TGrowthcycle.data';
import { list, deleteOne, batchDelete, getImportUrl, getExportUrl } from './TGrowthcycle.api';
import { downloadFile } from '/@/utils/common/renderUtils';
import Desc from "./components/Desc.vue";
import { useUserStore } from '@/store/modules/user';

const checkedKeys = ref<Array<string | number>>([]);
const userStore = useUserStore();
//注册model
const [registerModal, { openModal }] = useModal();
const [growthcyclePropertitesDrawer, { openDrawer: openGrowthcyclePropertitesDrawer }] = useDrawer();
// 详情内容
const [registerDesc, { openModal: openDescInfo }] = useModal();
//注册table数据
const { prefixCls, tableContext, onExportXls, onImportXls } = useListPage({
  tableProps: {
    title: '生长周期表',
    api: list,
    columns,
    canResize: false,
    formConfig: {
      //labelWidth: 120,
      schemas: searchFormSchema,
      autoSubmitOnEnter: true,
      showAdvancedButton: true,
      fieldMapToNumber: [
      ],
      fieldMapToTime: [
      ],
    },
    actionColumn: {
      width: 160,
      fixed: 'right'
    },
  },
  exportConfig: {
    name: "生长周期表",
    url: getExportUrl,
  },
  importConfig: {
    url: getImportUrl,
    success: handleSuccess
  },
})

const [registerTable, { reload }, { rowSelection, selectedRowKeys }] = tableContext

/**
 * 新增事件
 */
function handleAdd() {
  openModal(true, {
    isUpdate: false,
    showFooter: true,
  });
}
/**
 * 编辑事件
 */
function handleEdit(record: Recordable) {
  openModal(true, {
    record,
    isUpdate: true,
    showFooter: true,
  });
}
/**
 * 详情
*/
function handleDetail(record: Recordable) {
  openDescInfo(true, {
    record,
    isUpdate: true,
    showFooter: false,
  });
}
/**
 * 删除事件
 */
async function handleDelete(record) {
  await deleteOne({ id: record.id }, handleSuccess);
}
/**
 * 批量删除事件
 */
async function batchHandleDelete() {
  await batchDelete({ ids: selectedRowKeys.value }, handleSuccess);
}
/**
 * 成功回调
 */
function handleSuccess() {
  (selectedRowKeys.value = []) && reload();
}
/**
   * 生长周期属性
   */
   function handleGrowthcyclePropertites(record: Recordable) {
    openGrowthcyclePropertitesDrawer(true, record);
  }
/**
   * 操作栏
   */
function getTableAction(record) {
  const actions = [];
  if (record.createBy === userStore.getUserInfo.username) {
    actions.push({
      label: '编辑',
      onClick: handleEdit.bind(null, record),
    })
  }
  actions.push({
    label: '属性',
    onClick: handleGrowthcyclePropertites.bind(null, record),
  })
  return actions
}
/**
   * 下拉操作栏
   */
function getDropDownAction(record) {
  const actions = [{
    label: '详情',
    onClick: handleDetail.bind(null, record),
  }];
  if (record.createBy === userStore.getUserInfo.username) {
    actions.push({
      label: '删除',
      popConfirm: {
        title: '是否确认删除',
        confirm: handleDelete.bind(null, record),
      }
    })
  }
  return actions;
}


</script>
